package BandB.Tool.GPhoneManager.AppManager;

import BandB.Tool.GPhoneManager.ConfigManager;
import BandB.Tool.GPhoneManager.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Settings extends Activity implements OnClickListener {

	private LinearLayout mLayoutNotification;
	private LinearLayout mLayoutMultipleBackup;
	private LinearLayout mLayoutAirplane;
	private LinearLayout mLayoutBrightness;
	private LinearLayout mLayoutSilent;
	private LinearLayout mLayoutWifi;
	private LinearLayout mLayoutFile;
	private LinearLayout mLayoutAppList;
	private LinearLayout mLayoutAppGrid;
	private LinearLayout mLayoutTaskList;
	private LinearLayout mLayoutTaskGrid;

	private LinearLayout mLayoutShowBackup;
	private LinearLayout mLayoutShowSwitcher;
	private LinearLayout mLayoutShowApp;
	private LinearLayout modeBackup;
	private LinearLayout modeSwitcher;
	private LinearLayout modeApp;
	private ImageView imgBackup;
	private ImageView imgSwitcher;
	private ImageView imgApp;

	private CheckBox pinCheckBox;
	private CheckBox versionCheckBox;
	private CheckBox checkBoxAirplane;
	private CheckBox checkBoxBrightness;
	private CheckBox checkBoxSilent;
	private CheckBox checkBoxWifi;
	private CheckBox checkBoxFile;
	private CheckBox checkBoxAppList;
	private CheckBox checkBoxAppGrid;
	private CheckBox checkBoxTaskList;
	private CheckBox checkBoxTaskGrid;

	private ConfigManager mConfigManager;
	private NotificationManager mNotificationManager;

	private TextView rooTextView;
	private EditText pathEditText;

	public static final String APK_BACKUP_ROOT = Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ "/";
	public static final String BACKUP_PATH_DEFAULT_VALUE = "BandBTool/";
	protected static final String TAG = Settings.class.getSimpleName();

	public static final String ACTION_UPDATE_WIDGET = "update_gphone_widget";
	public static final String AIRPLANE_STATUS = "air";
	public static final String BRIGHT_STATUS = "brightness";
	public static final String SILENT_MODE_STATUS = "silent";
	public static final String WIFI_STATUS = "wifi";
	public static final String FILE_STATUS = "file";
	public static final String APP_LIST_STATUS = "appList";
	public static final String APP_GRID_STATUS = "appGrid";
	public static final String TASK_LIST_STATUS = "taskList";
	public static final String TASK_GRID_STATUS = "taskGrid";
	public static final int NOTIFY_ID = 1992;

	public static String DEFINED_PATH;

	private static boolean ENABLE_PIN_TO_NOTI_BAR;
	private static String BACKUP_PATH;
	private static boolean ENABLE_BACKUP_DIFFERENT_VERSION;
	private static boolean ENABLE_AIRPLANE;
	private static boolean ENABLE_BRIGHTNESS;
	private static boolean ENABLE_SILENT;
	private static boolean ENABLE_WIFI;

	private static boolean ENABLE_FILE;
	private static boolean ENABLE_APP_LIST;
	private static boolean ENABLE_APP_GRID;
	private static boolean ENABLE_TASK_LIST;
	private static boolean ENABLE_TASK_GRID;
	private static String[] INFOS;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("GPhone Mangager Settings");
		setContentView(R.layout.app_setting_view);

		initializeElements();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i(TAG, "onDestroy, save settings for your chooses.");
		saveSettings();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.setNotificationBar:
			boolean enableNotiBar = pinCheckBox.isChecked();
			pinCheckBox.setChecked(!enableNotiBar);
			if (!enableNotiBar) {
				showNotification();
			} else {
				cancelNotification();
			}
			break;
		case R.id.setMultipleBackup:
			boolean enableMultiplyBackup = versionCheckBox.isChecked();
			versionCheckBox.setChecked(!enableMultiplyBackup);
			break;
		case R.id.setAirplane:
			boolean enableAirplane = checkBoxAirplane.isChecked();
			checkBoxAirplane.setChecked(!enableAirplane);
			break;
		case R.id.setBrightness:
			boolean enableBrightness = checkBoxBrightness.isChecked();
			checkBoxBrightness.setChecked(!enableBrightness);
			break;
		case R.id.setSilent:
			boolean enableSilent = checkBoxSilent.isChecked();
			checkBoxSilent.setChecked(!enableSilent);
			break;
		case R.id.setWifi:
			boolean enableWifi = checkBoxWifi.isChecked();
			checkBoxWifi.setChecked(!enableWifi);
			break;
		case R.id.setFile:
			boolean enableFile = checkBoxFile.isChecked();
			checkBoxFile.setChecked(!enableFile);
			break;
		case R.id.setAppList:
			boolean enableAppList = checkBoxAppList.isChecked();
			checkBoxAppList.setChecked(!enableAppList);
			break;
		case R.id.setAppGrid:
			boolean enableAppGrid = checkBoxAppGrid.isChecked();
			checkBoxAppGrid.setChecked(!enableAppGrid);
			break;
		case R.id.setTaskList:
			boolean enableTaskList = checkBoxTaskList.isChecked();
			checkBoxTaskList.setChecked(!enableTaskList);
			break;
		case R.id.setTaskGrid:
			boolean enableTaskGrid = checkBoxTaskGrid.isChecked();
			checkBoxTaskGrid.setChecked(!enableTaskGrid);
			break;
		case R.id.showSwitcherSettings:
			if (View.VISIBLE == modeSwitcher.getVisibility()) {
				modeSwitcher.setVisibility(View.GONE);
				imgSwitcher.setBackgroundResource(R.drawable.show_more);
			} else {
				modeSwitcher.setVisibility(View.VISIBLE);
				imgSwitcher.setBackgroundResource(R.drawable.show_none);
			}
			break;
		case R.id.showBackupSettings:
			if (View.VISIBLE == modeBackup.getVisibility()) {
				modeBackup.setVisibility(View.GONE);
				imgBackup.setBackgroundResource(R.drawable.show_more);
			} else {
				modeBackup.setVisibility(View.VISIBLE);
				imgBackup.setBackgroundResource(R.drawable.show_none);
			}
			break;
		case R.id.showAppSettings:
			if (View.VISIBLE == modeApp.getVisibility()) {
				modeApp.setVisibility(View.GONE);
				imgApp.setBackgroundResource(R.drawable.show_more);
			} else {
				modeApp.setVisibility(View.VISIBLE);
				imgApp.setBackgroundResource(R.drawable.show_none);
			}
			break;
		default:
			break;
		}
	}

	private void showNotification() {
		Notification notification = new Notification(
				R.drawable.quick_uninstaller_icon, null, System
						.currentTimeMillis());
		Intent intent = new Intent(this, UninstallerList.class);
		intent.setAction(Intent.ACTION_SEARCH);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				intent, 0);
		notification.setLatestEventInfo(this, "GPhone manager",
				"Launch GPhone manager", contentIntent);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		mNotificationManager.notify(NOTIFY_ID, notification);
	}

	private void cancelNotification() {
		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancel(NOTIFY_ID);
	}

	private void initializeElements() {
		mConfigManager = ConfigManager.getInstance(this);
		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		INFOS = mConfigManager.getAllInfos();
		ENABLE_PIN_TO_NOTI_BAR = Boolean
				.valueOf(INFOS[ConfigManager.PIN_INDEX]);
		ENABLE_BACKUP_DIFFERENT_VERSION = Boolean
				.valueOf(INFOS[ConfigManager.VERSION_INDEX]);
		BACKUP_PATH = INFOS[ConfigManager.PATH_INDEX];
		ENABLE_AIRPLANE = Boolean.valueOf(INFOS[ConfigManager.AIRPLANE_INDEX]);
		ENABLE_BRIGHTNESS = Boolean
				.valueOf(INFOS[ConfigManager.BRIGHTNESS_INDEX]);
		ENABLE_SILENT = Boolean.valueOf(INFOS[ConfigManager.SILENT_INDEX]);
		ENABLE_WIFI = Boolean.valueOf(INFOS[ConfigManager.WIFI_INDEX]);
		ENABLE_FILE = Boolean.valueOf(INFOS[ConfigManager.FILE_INDEX]);
		ENABLE_APP_LIST = Boolean.valueOf(INFOS[ConfigManager.APP_LIST_INDEX]);
		ENABLE_APP_GRID = Boolean.valueOf(INFOS[ConfigManager.APP_GRID_INDEX]);
		ENABLE_TASK_LIST = Boolean
				.valueOf(INFOS[ConfigManager.TASK_LIST_INDEX]);
		ENABLE_TASK_GRID = Boolean
				.valueOf(INFOS[ConfigManager.TASK_GRID_INDEX]);

		mLayoutNotification = (LinearLayout) findViewById(R.id.setNotificationBar);
		mLayoutMultipleBackup = (LinearLayout) findViewById(R.id.setMultipleBackup);
		mLayoutAirplane = (LinearLayout) findViewById(R.id.setAirplane);
		mLayoutBrightness = (LinearLayout) findViewById(R.id.setBrightness);
		mLayoutSilent = (LinearLayout) findViewById(R.id.setSilent);
		mLayoutWifi = (LinearLayout) findViewById(R.id.setWifi);

		mLayoutFile = (LinearLayout) findViewById(R.id.setFile);
		mLayoutAppList = (LinearLayout) findViewById(R.id.setAppList);
		mLayoutAppGrid = (LinearLayout) findViewById(R.id.setAppGrid);
		mLayoutTaskList = (LinearLayout) findViewById(R.id.setTaskList);
		mLayoutTaskGrid = (LinearLayout) findViewById(R.id.setTaskGrid);

		mLayoutShowBackup = (LinearLayout) findViewById(R.id.showBackupSettings);
		mLayoutShowSwitcher = (LinearLayout) findViewById(R.id.showSwitcherSettings);
		mLayoutShowApp = (LinearLayout) findViewById(R.id.showAppSettings);
		modeBackup = (LinearLayout) findViewById(R.id.backupPathSettings);
		modeSwitcher = (LinearLayout) findViewById(R.id.switcherSettings);
		modeApp = (LinearLayout) findViewById(R.id.appSettings);
		imgBackup = (ImageView) findViewById(R.id.imgViewShowBackup);
		imgSwitcher = (ImageView) findViewById(R.id.imgViewShowSwitcher);
		imgApp = (ImageView) findViewById(R.id.imgViewShowApp);

		mLayoutNotification.setOnClickListener(this);
		mLayoutMultipleBackup.setOnClickListener(this);
		mLayoutAirplane.setOnClickListener(this);
		mLayoutBrightness.setOnClickListener(this);
		mLayoutSilent.setOnClickListener(this);
		mLayoutWifi.setOnClickListener(this);
		mLayoutFile.setOnClickListener(this);
		mLayoutAppList.setOnClickListener(this);
		mLayoutAppGrid.setOnClickListener(this);
		mLayoutTaskList.setOnClickListener(this);
		mLayoutTaskGrid.setOnClickListener(this);
		mLayoutShowBackup.setOnClickListener(this);
		mLayoutShowSwitcher.setOnClickListener(this);
		mLayoutShowApp.setOnClickListener(this);

		pinCheckBox = (CheckBox) findViewById(R.id.appmanager_pin_check);
		versionCheckBox = (CheckBox) findViewById(R.id.checkbox_different_version);
		checkBoxAirplane = (CheckBox) findViewById(R.id.checkBoxAirplane);
		checkBoxBrightness = (CheckBox) findViewById(R.id.checkBoxBrightness);
		checkBoxSilent = (CheckBox) findViewById(R.id.checkBoxSilent);
		checkBoxWifi = (CheckBox) findViewById(R.id.checkBoxWifi);
		checkBoxFile = (CheckBox) findViewById(R.id.checkBoxFile);
		checkBoxAppList = (CheckBox) findViewById(R.id.checkBoxAppList);
		checkBoxAppGrid = (CheckBox) findViewById(R.id.checkBoxAppGrid);
		checkBoxTaskList = (CheckBox) findViewById(R.id.checkBoxTaskList);
		checkBoxTaskGrid = (CheckBox) findViewById(R.id.checkBoxTaskGrid);

		pinCheckBox.setChecked(ENABLE_PIN_TO_NOTI_BAR);
		versionCheckBox.setChecked(ENABLE_BACKUP_DIFFERENT_VERSION);
		checkBoxAirplane.setChecked(ENABLE_AIRPLANE);
		checkBoxBrightness.setChecked(ENABLE_BRIGHTNESS);
		checkBoxSilent.setChecked(ENABLE_SILENT);
		checkBoxWifi.setChecked(ENABLE_WIFI);
		checkBoxFile.setChecked(ENABLE_FILE);
		checkBoxAppList.setChecked(ENABLE_APP_LIST);
		checkBoxAppGrid.setChecked(ENABLE_APP_GRID);
		checkBoxTaskList.setChecked(ENABLE_TASK_LIST);
		checkBoxTaskGrid.setChecked(ENABLE_TASK_GRID);

		rooTextView = (TextView) findViewById(R.id.root_text);
		rooTextView.setText(APK_BACKUP_ROOT);
		pathEditText = (EditText) findViewById(R.id.path_text);
		pathEditText.setText(BACKUP_PATH);

	}

	/**
	 * When quit this activity, we should save the settings
	 */
	private void saveSettings() {
		boolean notiBar = pinCheckBox.isChecked();
		boolean multiplyBackup = versionCheckBox.isChecked();
		String newPath = pathEditText.getText().toString();
		if (!newPath.endsWith("/")) {
			newPath = newPath + "/";
		}
		boolean airplane = checkBoxAirplane.isChecked();
		boolean brightness = checkBoxBrightness.isChecked();
		boolean silent = checkBoxSilent.isChecked();
		boolean wifi = checkBoxWifi.isChecked();
		boolean file = checkBoxFile.isChecked();
		boolean appList = checkBoxAppList.isChecked();
		boolean appGrid = checkBoxAppGrid.isChecked();
		boolean taskList = checkBoxTaskList.isChecked();
		boolean taskGrid = checkBoxTaskGrid.isChecked();

		if (notiBar != ENABLE_PIN_TO_NOTI_BAR) {
			mConfigManager.setPinToNotiBarEnabled(notiBar);
			// do not do following here, do following when click events invoked
			// if (notiBar) {
			// showNotification();
			// } else {
			// cancelNotification();
			// }
		}
		if (multiplyBackup != ENABLE_BACKUP_DIFFERENT_VERSION) {
			mConfigManager.setDifferentVersionEnabled(multiplyBackup);
		}

		if (!newPath.equals(BACKUP_PATH)) {
			mConfigManager.setPath(newPath);
		}
		/**
		 * Send this broadcast to notify that widget should do some changes.
		 */
		Intent i = new Intent();
		i.setAction(ACTION_UPDATE_WIDGET);

		if (airplane != ENABLE_AIRPLANE) {
			mConfigManager.setAirEnabled(airplane);
			i.putExtra(AIRPLANE_STATUS, airplane ? View.VISIBLE : View.GONE);
		}
		if (brightness != ENABLE_BRIGHTNESS) {
			mConfigManager.setBrightnessEnabled(brightness);
			i.putExtra(BRIGHT_STATUS, brightness ? View.VISIBLE : View.GONE);
		}
		if (silent != ENABLE_SILENT) {
			mConfigManager.setSilentEnabled(silent);
			i.putExtra(SILENT_MODE_STATUS, silent ? View.VISIBLE : View.GONE);
		}
		if (wifi != ENABLE_WIFI) {
			mConfigManager.setWifiEnabled(wifi);
			i.putExtra(WIFI_STATUS, wifi ? View.VISIBLE : View.GONE);
		}
		if (file != ENABLE_FILE) {
			mConfigManager.setFileManagerEnabled(file);
			i.putExtra(FILE_STATUS, file ? View.VISIBLE : View.GONE);
		}
		if (appList != ENABLE_APP_LIST) {
			mConfigManager.setAppListEnabled(appList);
			i.putExtra(APP_LIST_STATUS, appList ? View.VISIBLE : View.GONE);
		}
		if (appGrid != ENABLE_APP_GRID) {
			mConfigManager.setAppGridEnabled(appGrid);
			i.putExtra(APP_GRID_STATUS, appGrid ? View.VISIBLE : View.GONE);
		}
		if (taskList != ENABLE_TASK_LIST) {
			mConfigManager.setTaskListEnabled(taskList);
			i.putExtra(TASK_LIST_STATUS, taskList ? View.VISIBLE : View.GONE);
		}
		if (taskGrid != ENABLE_TASK_GRID) {
			mConfigManager.setTaskGridEnabled(taskGrid);
			i.putExtra(TASK_GRID_STATUS, taskGrid ? View.VISIBLE : View.GONE);
		}

		sendBroadcast(i);
	}
}
